package com.epam.edu.task03.parser;

import com.epam.edu.task03.dances.*;
import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Zhanna_Mukanova
 * Date: 9/20/13
 * Time: 9:41 AM
 * To change this template use File | Settings | File Templates.
 */
public class XMLParserDOM implements ParserFactory {
    private Logger logger = Logger.getLogger(XMLParserSAX.class);
    private List<Dance> dances = null;
    private List<Dancer> dancers = null;
    private Dance dance = null;
    private Dancer dancer = null;

    private static Element getBaby(Element parent, String childName) {
        NodeList nlist = parent.getElementsByTagName(childName);
        Element child = (Element) nlist.item(0);
        return child;
    }

    private static String getBabyValue(Element parent, String childName) {
        Element child = getBaby(parent, childName);
        Node node = child.getFirstChild();
        String value = node.getNodeValue();
        return value;
    }

    @Override
    public void getDancesDatas() {
        logger.info("________________XMLParserDOM_____________________");
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse("src/main/resources/dances.xml");
            Element root = document.getDocumentElement();
            dances = danceListBuilder(root);
        } catch (SAXException exception) {
            logger.error(exception.getMessage());
        } catch (ParserConfigurationException exception) {
            logger.error(exception.getMessage());
        } catch (IOException exception) {
            logger.error(exception.getMessage());
        }
        for (Dance dance : dances)
            logger.info(dance.toString());
    }

    public List<Dance> danceListBuilder(Element root) {
        List<Dance> innerDances = new LinkedList<Dance>();
        NodeList danceNotes = root.getElementsByTagName("dan:dance");
        for (int i = 0; i < danceNotes.getLength(); i++) {
            dance = new Dance();
            Element danceElement = (Element) danceNotes.item(i);
            dance.setDanceName(danceElement.getAttribute("danceName"));
            dance.setType(DanceType.fromValue(getBabyValue(danceElement, "dan:type")));
            dance.setScene(SceneType.fromValue(getBabyValue(danceElement,"dan:scene")));
            dance.setNumberOfDancers(NumberOfDancersType.fromValue(getBabyValue(danceElement,"dan:numberOfDancers")));
            dance.setMusic(MusicType.fromValue(getBabyValue(danceElement,"dan:music")));
            dance.setNumber(new Integer(getBabyValue(danceElement,"dan:number")));
            NodeList dancerNotes = danceElement.getElementsByTagName("dan:dancer");
            dancers = new LinkedList<Dancer>();
            for (int j = 0; j < dancerNotes.getLength(); j++) {
                dancer = new Dancer();
                Element dancerElement = (Element) danceNotes.item(j);
                dancer.setDancerName(getBabyValue(dancerElement, "dan:dancerName"));
                dancer.setNumberOfYears(new Integer(getBabyValue(danceElement,"dan:numberOfYears")));
                dancers.add(dancer);
            }
            innerDances.add(dance);
        }
        return innerDances;
    }
}

